﻿using fastCSharp;
using System;
using System.Collections.Generic;

namespace diantou.web.contest
{
    /// <summary>
    /// 比赛注册客户端缓存
    /// </summary>
    sealed class contestRegister : fastCSharp.sql.logStream<dataAccess.focusContest, dataModel.focus>.clientCustom
    {
        /// <summary>
        /// 比赛注册客户端缓存
        /// </summary>
        internal static readonly contestRegister Default = new contestRegister();
        /// <summary>
        /// 用户参与的比赛列表
        /// </summary>
        private Dictionary<int, list<int>> users = new Dictionary<int, list<int>>();
        /// <summary>
        /// 比赛的参与用户列表
        /// </summary>
        private Dictionary<int, list<int>> contests = new Dictionary<int, list<int>>();
        /// <summary>
        /// 重新加载数据
        /// </summary>
        /// <param name="values"></param>
        public override void Load(dataAccess.focusContest[] values)
        {
            users.Clear();
            contests.Clear();
            foreach (dataAccess.focusContest value in values.getSort(value => value.FocusTime)) Insert(value);
        }
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="value"></param>
        public override void Insert(dataAccess.focusContest value)
        {
            list<int> ids;
            if (!users.TryGetValue(value.UserId, out ids)) users.Add(value.UserId, ids = new list<int>());
            ids.Add(value.FocusId);
            if (!contests.TryGetValue(value.FocusId, out ids)) contests.Add(value.FocusId, ids = new list<int>());
            ids.Add(value.UserId);
        }
        /// <summary>
        /// 获取用户标识集合
        /// </summary>
        /// <param name="contestId"></param>
        /// <returns></returns>
        internal list<int> GetUserIds(int contestId)
        {
            list<int> ids;
            return contests.TryGetValue(contestId, out ids) ? ids : null;
        }
        /// <summary>
        /// 获取竞赛标识集合
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        internal list<int> GetContestIds(int userId)
        {
            list<int> ids;
            return users.TryGetValue(userId, out ids) ? ids : null;
        }
    }
}
